package cc.yiueil.config;

import cc.yiueil.filter.JwtFilter;
import cc.yiueil.handler.ApplicationAccessDeniedHandler;
import cc.yiueil.handler.ApplicationLoginFailHandler;
import cc.yiueil.handler.ApplicationLoginSuccessHandler;
import cc.yiueil.handler.ApplicationLogoutSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.List;

@Configuration
@EnableMethodSecurity
public class SecurityConfig {
    @Autowired
    JwtFilter jwtFilter;

    @Autowired
    ApplicationLoginSuccessHandler applicationLoginSuccessHandler;

    @Autowired
    ApplicationLoginFailHandler applicationLoginFailHandler;

    @Autowired
    ApplicationLogoutSuccessHandler applicationLogoutSuccessHandler;

    @Autowired
    ApplicationAccessDeniedHandler applicationAccessDeniedHandler;

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowedHeaders(List.of("*"));
        corsConfiguration.setAllowedMethods(List.of("*"));
        corsConfiguration.setAllowedOrigins(List.of("*"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return source;
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
                // 跨站请求伪造防御关闭
                .csrf(AbstractHttpConfigurer::disable)
                // cors配置允许跨域
                .cors(cors -> cors.configurationSource(corsConfigurationSource()))
                // 使用无状态的Session
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                // 配置登录地址, 成功以及失败的Handler
                .formLogin(formLogin -> formLogin
                        .loginProcessingUrl("/login")
                        .successHandler(applicationLoginSuccessHandler)
                        .failureHandler(applicationLoginFailHandler))
                // 配置登出地址, 以及登出成功的Handler
                .logout(logout -> logout
                        .logoutUrl("/logout")
                        .logoutSuccessHandler(applicationLogoutSuccessHandler))
                // 配置授权异常处理Handler
                .exceptionHandling(exceptionHandlingConfigurer -> exceptionHandlingConfigurer
                        .accessDeniedHandler(applicationAccessDeniedHandler))
                .build();
    }
}
